import React, { Component } from 'react';
import { Form, Input, Button, Row, Col, message } from 'antd';
import { connect } from 'dva';
import GetYCodeFormItem from '../../components/GetYCodeFormItem';
const FormItem = Form.Item;

@Form.create()
@connect(({ loading }) => ({
  loading: loading.effects['bindPhone/setBindPhone'],
}))
class BindPhone extends Component {
  handleSubmit = e => {
    e.preventDefault();
    const { form, dispatch, changeStatus } = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        dispatch({
          type: 'bindPhone/setBindPhone',
          payload: values,
        })
          .then(msg => {
            changeStatus(true);
          })
          .catch(errMsg => {
            message.error(errMsg);
          });
      }
    });
  };
  render() {
    const {
      form: { getFieldDecorator },
      form,
      loading,
    } = this.props;
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
    };
    return (
      <Form onSubmit={this.handleSubmit}>
        <Row>
          <Col span={8}>
            <FormItem label="手机号码" {...formItemLayout}>
              {getFieldDecorator('phone', {
                rules: [{ required: true, message: '请输入手机号码!' }],
              })(<Input placeholder="手机号码" />)}
            </FormItem>
          </Col>
        </Row>

        <GetYCodeFormItem form={form} validField="phone" formItemLayout={formItemLayout} />

        <Row>
          <Col span={8} offset={2}>
            <Button type="primary" htmlType="submit" disabled={loading}>
              {loading ? '请稍后' : '下一步'}
            </Button>
          </Col>
        </Row>
      </Form>
    );
  }
}
export default BindPhone;
